<?php
namespace app\api\controller;

use app\api\extend\Common;

/**
 * 用户控制器
 */
class HomeMuser extends Common {
    /**
     * 注册
     * @return array|\think\response\Json
     */
	public function register() {
        // 验证用户注册信息
        $res = $this->check_params();
        if (!empty($res)) {
           return $this->return_msg(400,$res); 
        }
        $data=[
            'name' => $this->params['name'],
            'password' => $this->encipher($this->params['password']),
            'user_type' => $this->params['user_type'],
        ];
       
        $res = $this->model->register($data);
        if (!$res){
            return $this->return_msg(400,$this->getConst(false));
        }
        return $this->return_msg(200,$this->getConst());
	}
    /**
     * 登录
     * @return array|\think\response\Json
     */
	public function login() {
        // 验证用户登录信息
        $res = $this->check_params();
        if (!empty($res)) {
           return $this->return_msg(400,$res); 
        }
        $data=[
            'name' => $this->params['name'],
            'password' => $this->encipher($this->params['password']),
            'user_type' => $this->params['user_type'],
        ];
        $res = $this->model->login($data);
        if (!$res){
            return $this->return_msg(400,$this->getConst(false));
        }
        //生成token
        $token = $this->generateToken($res['id'],$this->key);
        return $this->return_msg(200,$this->getConst(),['token'=>$token],false);
	}
    /**
     * 获获取用户信息
     */
    public function info() {
        $res = $this->model->info($this->request->auth['userId']);
        if (!$res){
            return $this->return_msg(400,'获取用户信息失败');
        }
        return $this->return_msg(200,'获取用户信息成功',$res);
    }
    
}
